package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWInputBox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UIHelpGong implements IUIWindows
{

	public DWFrame m_help_frame = null;
	public Bitmap m_cr_tit = null;
	public DWImageBox m_tuichu_ima = null;
	public Bitmap m_cr_tuichu = null;
	public DWBackground m_xbj = null;
	public Bitmap m_cr_left = null;
	public Bitmap m_cr_right = null;
	public Bitmap m_cr_but = null;
	public Bitmap m_cr_but_down = null;
	public DWImageBox m_left_ima = null;
	public DWImageBox m_right_ima = null;
	public DWButton m_but = null;
	public DWInputBox m_input = null;
	public DWTextbox m_must_text = null;
	public DWTextbox m_count_text = null;
	public DWImageBox m_jinbi_ima = null;
	public Bitmap m_cr_jinbi = null;
	int m_count = 0;
	public void HelpGong()
	{
		if (DWControlsManager.getInstance().contains(m_help_frame)) {
			if (m_help_frame != null) {
				DWControlsManager.getInstance().removeControl(m_help_frame);
			}
		}
		if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH)
		{
			m_help_frame = new DWFrame(500, 400);
		}
		else
		{
			m_help_frame = new DWFrame(400, 300);
		}
		m_help_frame.setClickClose(false);
		m_help_frame.setCentre();
		DWControlsManager.getInstance().addControl(m_help_frame);

		DWTitle m_tit = new DWTitle("", m_help_frame);
		DWImageBox m_tit_ima = new DWImageBox(m_cr_tit);
		m_tit_ima.setNearAnchor(m_tit, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);

		m_tuichu_ima = new DWImageBox(m_cr_tuichu);
		m_tuichu_ima.setNearAnchor(m_tit, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER, 0, 0);
		m_tuichu_ima.setTouchZoomIn(20, 20);
		m_tuichu_ima.addListener(new DWListener() {

			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWControlsManager.getInstance().removeControl(m_help_frame);
			}
		});

		m_xbj = new DWBackground(m_help_frame.getShowWidth() - 20, m_help_frame.getShowHeight()
				- (m_tit.getShowHeight() - 20) - 20);
		m_xbj.setFill(true);
		m_xbj.setNearAnchor(m_help_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, (m_tit.getShowHeight() - 20) + 10);

		DWTextbox m_tb = new DWTextbox("<mark c=55FF00>"+"兑换帮贡数量："+"</mark>");
		m_tb.setNearAnchor(m_xbj, Tools.LEFT_TOP, Tools.LEFT_TOP, 20, 20);

		m_left_ima = new DWImageBox(m_cr_left);
		m_left_ima.setNearAnchor(m_tb, Tools.RIGHT_TOP, Tools.RIGHT_BOTTOM, 0, 15);
		m_left_ima.setTouchZoomIn(15, 20);
		m_left_ima.addListener(m_left_lis);

		m_input = new DWInputBox(m_xbj.getShowWidth() - 40 - m_tb.getShowWidth() - m_cr_right.getWidth() - 30,
				m_cr_right.getHeight() - 10, m_help_frame);
		m_input.setText(Integer.toString(m_longest));
		m_input.addListener(new DWListener() {
			
			public void OnDoubleClick()
			{
			}
			
			public void OnClick()
			{
				if(DWControlsManager.getInstance().contains(m_help_frame))
				{
					if(m_must_text != null)
					{
						int m_gold = Integer.parseInt(m_input.getText())*m_huilv;
						m_must_text.setText("需要"+"<mark c=ffc001>"+m_gold+"</mark>");
						m_jinbi_ima.setNearAnchor(m_must_text, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 5, 0);
					}
					if(m_count_text != null)
					{
						if(DWGameManager.getInstance().m_role.m_vip_lv==0)
						{
							m_count_text.setText("<mark c=ffc001>"+"非VIP玩家，今日还可兑换"+"</mark>"+"<mark c=55FF00>"+m_longest+"</mark>"+"<mark c=ffc001>"+"帮贡"+"</mark>");
						}
						else if(DWGameManager.getInstance().m_role.m_vip_lv>0)
						{
							m_count_text.setText("<mark c=ffc001>"+"VIP"+DWGameManager.getInstance().m_role.m_vip_lv+"玩家，今日还可兑换"+"</mark>"+"<mark c=55FF00>"+m_longest+"</mark>"+"<mark c=ffc001>"+"帮贡"+"</mark>");
						}
					}
				}
			}
		});
		m_input.setNearAnchor(m_left_ima, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 15, 0);

		m_right_ima = new DWImageBox(m_cr_right);
		m_right_ima.setNearAnchor(m_input, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 15, 0);
		m_right_ima.setTouchZoomIn(15, 20);
		m_right_ima.addListener(m_right_lis);

		m_must_text = new DWTextbox("需要"+"<mark c=ffc001>"+m_gold+"</mark>");
		m_must_text.setNearAnchor(m_tb, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 10, m_left_ima.getShowHeight() + 30);

		m_jinbi_ima = new DWImageBox(m_cr_jinbi);
		m_jinbi_ima.setNearAnchor(m_must_text, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 5, 0);
		
		m_count_text = new DWTextbox("<mark c=ffc001>"+"今日还可兑换"+"</mark>"+"<mark c=55FF00>"+m_longest+"</mark>"+"<mark c=ffc001>"+"帮贡"+"</mark>", m_xbj.getShowWidth() - 40);
		m_count_text.setNearAnchor(m_must_text, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, -10, 10);

		m_but = new DWButton("确定", m_cr_but);
		m_but.setDownImage(m_cr_but_down);
		m_but.setNearAnchor(m_count_text, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, -10, 15);
		m_but.addListener(new DWListener() {
			
			public void OnDoubleClick()
			{}
			
			public void OnClick()
			{
				int m_gold=Integer.parseInt(m_input.getText())*m_huilv;
				m_count = Integer.parseInt(m_input.getText());
				if(m_gold > DWGameManager.getInstance().m_role.m_gold)
				{
					DWGameManager.getInstance()
					.addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "抱歉，您的金币不足"+m_gold+"，无法购买"+m_count+"帮贡。");
				}
				else
				{
					if(m_longest == 0)
					{
						DWGameManager.getInstance()
						.addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "抱歉，您今日的次数已经用尽。");
					}
					else if(m_longest>0)
					{
						if(m_count>0)
						{
							DWGameManager.getInstance().getSendMessage().sendHelpBuy(m_count);
						}
						else
						{
							DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "抱歉,输入数量错误。");
						}
					}
				}
			}
		});
		
		m_help_frame.addControl(m_tit);
		m_help_frame.addControl(m_tit_ima);
		m_help_frame.addControl(m_tuichu_ima);
		m_help_frame.addControl(m_xbj);
		m_help_frame.addControl(m_tb);
		m_help_frame.addControl(m_left_ima);
		m_help_frame.addControl(m_right_ima);
		m_help_frame.addControl(m_input);
		m_help_frame.addControl(m_must_text);
		m_help_frame.addControl(m_jinbi_ima);
		m_help_frame.addControl(m_count_text);
		m_help_frame.addControl(m_but);
		
	}

	private DWListener m_left_lis = new DWListener() {

		public void OnDoubleClick()
		{
		}

		public void OnClick()
		{
			if(m_input != null)
			{
				int m_left_input = Integer.parseInt(m_input.getText());
				if(m_left_input>0)
				{
					int m_set_left_input = m_left_input-1;
					m_input.setText(Integer.toString(m_set_left_input));
					m_must_text.setText("需要"+"<mark c=ffc001>"+m_set_left_input*m_huilv+"</mark>");
					m_jinbi_ima.setNearAnchor(m_must_text, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 5, 0);
				}
				else
				{
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "当前数量为0，操作无效。");
				}
			}
		}
	};
	
	private DWListener m_right_lis = new DWListener() {

		public void OnDoubleClick()
		{
		}

		public void OnClick()
		{
			if(m_input != null)
			{
				int m_right_input = Integer.parseInt(m_input.getText());
				if(m_right_input>=0 && m_right_input<m_longest)
				{
					int m_set_right_input = m_right_input+1;
					m_input.setText(Integer.toString(m_set_right_input));
					m_must_text.setText("需要"+"<mark c=ffc001>"+m_set_right_input*m_huilv+"</mark>");
					m_jinbi_ima.setNearAnchor(m_must_text, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 5, 0);
				}
				else
				{
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "当前数量为今日购买上限，操作无效。");
				}
			}
		}
	};

	public void init()
	{
		m_cr_tit = UIWindows.createImage("/img/newui/duihuanbanggong.gnp");
		m_cr_tuichu = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		m_cr_left = UIWindows.createImage("/img/newui/haoyoufyan_2.gnp");///个数翻页
		m_cr_right = UIWindows.createImage("/img/newui/haoyoufyan_1.gnp");
		m_cr_jinbi = UIWindows.createImage("/img/newui/jinbi_1.gnp");
		m_cr_but = UIWindows.createImage("/img/newui/anniu_6.gnp");//客服的发送按钮
		m_cr_but_down = UIWindows.createImage("/img/newui/anniu_6ax.gnp");
	}

	public void open(byte index)
	{
		HelpGong();
	}

	public void close(byte index)
	{
	}

	public void dispose()
	{
	}

	public void logic()
	{
	}
	
  public int m_longest = 0;
  public int m_huilv = 0;
  public byte m_type;
  int m_gold;
  public void recvHelpGongCount(DataInputStream dis) throws IOException
  {
	  m_type = dis.readByte();
	  if(m_type == 1)
	  {
		  m_longest = dis.readInt();
		  m_huilv = dis.readInt();
		  m_gold = m_longest*m_huilv;
		  if(m_longest>0)
		  {
			  if(DWControlsManager.getInstance().contains(m_help_frame))
			  {
				  if(m_input != null)
				  {
					  m_input.setText(Integer.toString(m_longest));
				  }
				  if(m_must_text != null)
				  {
					  m_must_text.setText("需要"+"<mark c=ffc001>"+m_gold+"</mark>");
					  m_jinbi_ima.setNearAnchor(m_must_text, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 5, 0);
				  }
				  if(m_count_text != null)
				  {
					  if(DWGameManager.getInstance().m_role.m_vip_lv==0)
					  {
						  m_count_text.setText("<mark c=ffc001>"+"非VIP玩家，今日还可兑换"+"</mark>"+"<mark c=55FF00>"+m_longest+"</mark>"+"<mark c=ffc001>"+"帮贡"+"</mark>");
					  }
					  else if(DWGameManager.getInstance().m_role.m_vip_lv>0)
					  {
						  m_count_text.setText("<mark c=ffc001>"+"VIP"+DWGameManager.getInstance().m_role.m_vip_lv+"玩家，今日还可兑换"+"</mark>"+"<mark c=55FF00>"+m_longest+"</mark>"+"<mark c=ffc001>"+"帮贡"+"</mark>");
					  }
				  }
			  }
			  else if(!DWControlsManager.getInstance().contains(m_help_frame))
			  {
				  open((byte)0);
			  }
		  }
		  else if(m_longest == 0)
		  {
			  if(DWControlsManager.getInstance().contains(m_help_frame))
			  {
				  if(m_input != null)
				  {
					  m_input.setText(Integer.toString(m_longest));
				  }
				  if(m_must_text != null)
				  {
					  m_must_text.setText("需要"+"<mark c=ffc001>"+m_gold+"</mark>");
					  m_jinbi_ima.setNearAnchor(m_must_text, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 5, 0);
				  }
				  if(m_count_text != null)
				  {
					  if(DWGameManager.getInstance().m_role.m_vip_lv==0)
					  {
						  m_count_text.setText("<mark c=ffc001>"+"非VIP玩家，今日还可兑换"+"</mark>"+"<mark c=55FF00>"+m_longest+"</mark>"+"<mark c=ffc001>"+"帮贡"+"</mark>");
					  }
					  else if(DWGameManager.getInstance().m_role.m_vip_lv>0)
					  {
						  m_count_text.setText("<mark c=ffc001>"+"VIP"+DWGameManager.getInstance().m_role.m_vip_lv+"玩家，今日还可兑换"+"</mark>"+"<mark c=55FF00>"+m_longest+"</mark>"+"<mark c=ffc001>"+"帮贡"+"</mark>");
					  }
				  }
			  }
			  else if(!DWControlsManager.getInstance().contains(m_help_frame))
			  {
				  DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "抱歉,您今日的次数已经用尽。");
			  }
		  }
		  Tools.debugPrintln("状态"+m_type);
		  Tools.debugPrintln("还可兑换的数量值"+m_longest);
		  Tools.debugPrintln("汇率："+m_huilv);
	  }
	  else
	  {
		  DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "获取网络信息失败。");
	  }
  }
  
  public void recvSubmitBuy(DataInputStream dis) throws IOException
  {
	  byte m_status = dis.readByte();
	  if(m_status == 0)
	  {
		  DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "抱歉，购买帮贡失败。");
	  }
	  else if(m_status == 1)
	  {
		  DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "恭喜，成功购买"+m_count+"帮贡。");
		  DWGameManager.getInstance().getSendMessage().sendHelpCount();
	  }
	  Tools.debugPrintln("状态"+m_status);
  }
}
